package cn.jinjimi.app.main.activity;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.Constants;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.controller.event.AnalysisEvent;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.domain.data.dto.ArticleListDTO;
import cn.jinjimi.app.entity.Article;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ShowAnalysisActivity extends ToolBarCommonActivity {
    private TextView readedCount;
    private TextView percent;
    private TextView shareCount;
    private ListView hotList;
    private ArticleListDTO articleListDTO;
    private String readCount;
    private RelativeLayout noShare;
    private LinearLayout hasShare;

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_show_analysis);
        super.setTitle(R.string.me_analysis_title);
    }

    @Override
    protected void initial() {
        initView();
        initData();
    }

    protected void initView() {
        TextView visitedCount0 = (TextView) findViewById(R.id.tv_visited_0);
        TextView visitedCount1 = (TextView) findViewById(R.id.tv_visited_1);
        TextView visitedCount2 = (TextView) findViewById(R.id.tv_visited_2);
        TextView visitedCount3 = (TextView) findViewById(R.id.tv_visited_3);
        TextView visitedCount4 = (TextView) findViewById(R.id.tv_visited_4);
        readedCount = (TextView) findViewById(R.id.tv_count_readed);
        percent = (TextView) findViewById(R.id.tv_percent_of_all);
        shareCount = (TextView) findViewById(R.id.tv_count_share);
        hotList = (ListView) findViewById(R.id.list_hot_articles);
        noShare = (RelativeLayout) findViewById(R.id.rl_buttom_noshare);
        hasShare = (LinearLayout) findViewById(R.id.ll_buttom_share);
        hotList.setFocusable(false);

        counts = new TextView[]{visitedCount0, visitedCount1, visitedCount2, visitedCount3, visitedCount4};
    }

    private TextView[] counts;

    public void initData() {
        getSelfData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAnalysisEvent(AnalysisEvent event) {
        if (isFinishing())
            return;

        switch (event.state) {
            case DataEvent.SUCC:
                articleListDTO = event.data;
                if (articleListDTO != null) {
                    hotList.setAdapter(new MyAdapter());
                }
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(getApplication(), "网络出错，请稍后再试");
                break;
            case DataEvent.HEADER:
                readCount = event.header.get("read_count");
                final String total = event.header.get("total");
                final String weekClick = event.header.get("week_clicks");
                final String percentStr = event.header.get("percent");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapterCount(weekClick);
                        percent.setText(percentStr + "%");
                        readedCount.setText(readCount);
                        shareCount.setText(total);
                        if (!total.equals(Constants.ZERO)) {
                            hasShare.setVisibility(View.VISIBLE);
                            noShare.setVisibility(View.INVISIBLE);
                        }
                    }
                });
                break;
        }
        hideLoadingIndicator();
    }

    private void getSelfData() {
        showLoadingIndicator();

    }

    public void adapterCount(String count) {
        char[] characters = count.toCharArray();
        for (int i = characters.length - 1, j = 0; i >= 0; i--, j++) {
            counts[j].setText(String.valueOf(characters[i]));
        }
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return articleListDTO.getArticles().size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final Article article = articleListDTO.getArticles().get(position);
            convertView = View.inflate(getApplication(), R.layout.item_hot_share_articles, null);
            TextView title = (TextView) convertView.findViewById(R.id.tv_title_article);
            TextView count = (TextView) convertView.findViewById(R.id.tv_count_readed);
            title.setText(article.getTitle());
            count.setText(article.getReadNum() + "");
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ShowAnalysisActivity.this, ReaderDetailActivity.class);
                    intent.putExtra(BundleKey.ARTICLE_ID, article.getId());
                    ShowAnalysisActivity.this.startActivity(intent);
                }
            });
            return convertView;
        }
    }

}
